﻿using System;
using System.Configuration;
using AppDomainToolkit;
using MongoDB.Bson;
using ZilLion.Core.TaskManager.Client.Quartz;
using ZilLion.TaskManager.Entity.Entity;


namespace ZilLion.Core.TaskManager.Client.Core
{
    [Serializable]
    public  class TaskExcuteProvider:MarshalByRefObject
    {
        private  readonly TaskNodeConfig _nodeConfig = new TaskNodeConfig()
        {
            NodeConfigDbConString = ConfigurationManager.AppSettings["NodeConfigDbConString"] ,
            TaskServerId = ConfigurationManager.AppSettings["NodeId"]
        };



        private static TaskExcuteProvider _taskExcuteProvider;

        private TaskExcuteProvider()
        {

        }


        public static TaskExcuteProvider GetInstance()
        {
            if (_taskExcuteProvider==null)
                _taskExcuteProvider=new TaskExcuteProvider();
            return _taskExcuteProvider;
        }

        /// <summary>
        ///     开始任务
        /// </summary>
        /// <param name="config"></param>
        /// <returns></returns>
        public  void StartTask(TaskConfig config)
        {
            //获取任务配置模块
            var appDomainContext = TaskRuntimePool.GetInstance().GetAppDomainContext(config.TaskModule);
            if (appDomainContext == null) //如果appdomain没有启动，则需要启动并初始化InitScheduler
            {
                TaskRuntimePool.GetInstance().Add(config.TaskModule);
                appDomainContext = TaskRuntimePool.GetInstance().GetAppDomainContext(config.TaskModule);
                RemoteAction.Invoke(appDomainContext.Domain, config, _nodeConfig, (taskconfig, runnerconfig) =>
                {
                    var container = SchedulerContainer.GetContainerInstance();
                    container.InitScheduler(taskconfig);
                    container.StartScheduler(); //开始StartScheduler
                });
            }


            RemoteAction.Invoke(appDomainContext.Domain, config, taskconfig =>
            {
                var container = SchedulerContainer.GetContainerInstance();
                container.StartTask(taskconfig); //开始任务
            });

        
        }

        /// <summary>
        ///     暂停任务
        /// </summary>
        /// <returns></returns>
        public  void PauseTask(TaskConfig config)
        {
            var appDomainContext = TaskRuntimePool.GetInstance().GetAppDomainContext(config.TaskModule);

          
            if (appDomainContext == null) //如果appdomain没有启动，则需要启动并初始化InitScheduler
            {
                TaskRuntimePool.GetInstance().Add(config.TaskModule);
                appDomainContext = TaskRuntimePool.GetInstance().GetAppDomainContext(config.TaskModule);
                RemoteAction.Invoke(appDomainContext.Domain, config, _nodeConfig, (taskconfig, runnerconfig) =>
                {
                    var container = SchedulerContainer.GetContainerInstance();
                    container.InitScheduler(taskconfig);
                    container.StartScheduler(); //开始StartScheduler
                });
            }


            RemoteAction.Invoke(appDomainContext.Domain, config, taskconfig =>
            {
                var container = SchedulerContainer.GetContainerInstance();
                container.StopTask(taskconfig);
            });

        }

        /// <summary>
        ///     卸载任务
        /// </summary>
        /// <param name="config"></param>
        /// <returns></returns>
        public  bool DeleteTask(TaskConfig config)
        {
            var appDomainContext = TaskRuntimePool.GetInstance().GetAppDomainContext(config.TaskModule);
            if (appDomainContext == null) //如果appdomain没有启动，则需要启动并初始化InitScheduler
            {
                TaskRuntimePool.GetInstance().Add(config.TaskModule);
                appDomainContext = TaskRuntimePool.GetInstance().GetAppDomainContext(config.TaskModule);
                RemoteAction.Invoke(appDomainContext.Domain, config, _nodeConfig, (taskconfig, runnerconfig) =>
                {
                    var container = SchedulerContainer.GetContainerInstance();
                    container.InitScheduler(taskconfig);
                    container.StartScheduler(); //开始StartScheduler
                });
            }
            RemoteAction.Invoke(appDomainContext.Domain, config, taskconfig =>
            {
                var container = SchedulerContainer.GetContainerInstance();
                container.DeleteTask(config);
            });
        
            return true;
        }

        /// <summary>
        ///     释放任务资源
        /// </summary>
        /// <param name="taskModule"></param>
        /// <returns></returns>
        public  bool UninstallSchedule(string taskModule)
        {
            var appDomainContext = TaskRuntimePool.GetInstance().GetAppDomainContext(taskModule);
            if (appDomainContext == null) return true; //如果appdomain没有启动，可以无视
            RemoteAction.Invoke(appDomainContext.Domain, () =>
            {
                var container = SchedulerContainer.GetContainerInstance();
                container.StopSchedule();
            });
            return true;
        }
    }
}